package com.zzz.web.tools;


import com.zzz.web.Data.RSSI;
import com.zzz.web.Data.XYaxis;
import com.zzz.web.service.RssiService;
import org.junit.Test;

import java.nio.DoubleBuffer;
import java.util.ArrayList;
import java.util.List;

public class tool {
    private void tools() {
    }

    private static int row = 11;
    private static int column = 9;
    private static final double thresholdValue = 0.0;

    /**
     * 使用二维矩阵来记录RSSI，方便更新和匹配
     */
    private static RSSI[][] RSSIs = new RSSI[row][column];

    /**
     * 矩阵初始化
     */
    static {
        int rssinum = 0;
        List<RSSI> allRSSI = new RssiService().all();
        for (int i = 0; i < row; i += 2) {
            for (int i1 = 0; i1 < column; i1 += 2) {
                RSSIs[i][i1] = allRSSI.get(rssinum);
            }
        }
    }

    public static RSSI[][] getCurrentRSSIs() {
        return RSSIs;
    }

    public static void setCurrentRSSIs(RSSI rssi, int x, int y) {
        RSSIs[x][y] = rssi;
    }


    public static RSSI getRssi(String rssi) {
        RSSI temprssi = new RSSI();
        String[] datas = rssi.split(",");
        temprssi.setNumber(Integer.parseInt(datas[0].split("=")[1]));
        temprssi.setFrequency(Integer.parseInt(datas[1].split("=")[1]));

        temprssi.setAP1MAC(datas[2].split("=")[1]);
        temprssi.setAP1RSSI(Integer.parseInt(datas[3].split("=")[1]));

        temprssi.setAP2MAC(datas[4].split("=")[1]);
        temprssi.setAP2RSSI(Integer.parseInt(datas[5].split("=")[1]));

        temprssi.setAP3MAC(datas[6].split("=")[1]);
        temprssi.setAP3RSSI(Integer.parseInt(datas[7].split("=")[1]));

        return temprssi;
    }

    // 从发送来的数据中获得RSSI值
    public static RSSI getRssi(String number, String other) {
        RSSI rssi = new RSSI();
        rssi.setNumber(Integer.parseInt(number));
        rssi.setFrequency(0);

        String[] tempstring = new String[6];
        int i = 0;
        String[] AP = other.split(" ");
        for (String ap : AP) {
            String[] strings = ap.split(",");
            tempstring[i] = strings[0];
            tempstring[i + 1] = strings[1];
            i += 2;
        }
        rssi.setAP1MAC(tempstring[0]);
        rssi.setAP1RSSI(Integer.parseInt(tempstring[1]));

        rssi.setAP2MAC(tempstring[2]);
        rssi.setAP2RSSI(Integer.parseInt(tempstring[3]));

        rssi.setAP3MAC(tempstring[4]);
        rssi.setAP3RSSI(Integer.parseInt(tempstring[5]));

        return rssi;
    }

    // 平均算法
    public static RSSI avgRSSI(List<RSSI> rssiArrayList) {
        int avgAP1RSSI = 0, avgAP2RSSI = 0, avgAP3RSSI = 0;

        for (RSSI rssi : rssiArrayList) {
            avgAP1RSSI += rssi.getAP1RSSI();
            avgAP2RSSI += rssi.getAP2RSSI();
            avgAP3RSSI += rssi.getAP3RSSI();
        }

        RSSI avgrssi = new RSSI();
        avgrssi.setNumber(rssiArrayList.get(0).getNumber());
        avgrssi.setAP1MAC(rssiArrayList.get(0).getAP1MAC());
        avgrssi.setAP2MAC(rssiArrayList.get(0).getAP2MAC());
        avgrssi.setAP3MAC(rssiArrayList.get(0).getAP3MAC());

        avgAP1RSSI = avgAP1RSSI / rssiArrayList.size();
        avgrssi.setAP1RSSI(avgAP1RSSI);
        avgAP2RSSI = avgAP2RSSI / rssiArrayList.size();
        avgrssi.setAP2RSSI(avgAP2RSSI);
        avgAP3RSSI = avgAP3RSSI / rssiArrayList.size();
        avgrssi.setAP3RSSI(avgAP3RSSI);

        return avgrssi;
    }

    public static XYaxis getXYFromNum(int num) {
        return new XYaxis(num / 6, num - num / 6 * 5 - 1);
    }

    public static boolean isClose(RSSI r1, RSSI r2) {
        double r1RssiSquare = Math.pow(r1.getAP1RSSI(), 2) +
                Math.pow(r1.getAP2RSSI(), 2) +
                Math.pow(r1.getAP3RSSI(), 2);
        double r2RssiSquare = Math.pow(r2.getAP1RSSI(), 2) +
                Math.pow(r2.getAP2RSSI(), 2) +
                Math.pow(r2.getAP3RSSI(), 2);
        double r1_r2InnerProduct = r1.getAP1RSSI() * r2.getAP1RSSI() +
                r1.getAP2RSSI() * r2.getAP2RSSI() +
                r1.getAP3RSSI() * r2.getAP3RSSI();

        if (r1_r2InnerProduct / (r1RssiSquare * r2RssiSquare) > thresholdValue) {
            return false;
        }
        return true;
    }
}
